package com.spic.business.bp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spic.business.alarm.domain.DeviceInfoVo;
import com.spic.business.alarm.domain.EmpLocation;
import com.spic.business.alarm.mapper.EmpLocationMapper;
import com.spic.business.alarm.mapper.WeatherDataMapper;
import com.spic.business.alarm.service.DeviceService;
import com.spic.business.alarm.utils.HttpClientUtil;
import com.spic.business.bp.constant.BpConstant;
import com.spic.business.bp.domain.*;
import com.spic.business.bp.mapper.BpAlarmMapper;
import com.spic.business.bp.mapper.BpGnssMapper;
import com.spic.business.bp.mapper.BpInfoAlarmMapper;
import com.spic.business.bp.mapper.BpPositionDataMapper;
import com.spic.business.bp.service.BpService;
import com.spic.business.common.core.utils.DateUtil;
import com.spic.business.getui.service.PushService;
import com.spic.business.hik.constant.HkConstant;
import com.spic.business.hik.utils.HkResultUtlis;
import com.spic.business.preset_point.domain.PresetsPosition;
import com.spic.business.preset_point.service.IPresetsPositionService;
import com.spic.common.core.constant.SecurityConstants;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.StringUtils;
import com.spic.system.api.RemoteDictDataService;
import com.spic.system.api.RemoteNoticeInfoService;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysDictData;
import com.spic.system.api.domain.SysNoticeInfo;
import com.spic.system.api.model.LoginUser;
import com.vividsolutions.jts.algorithm.ConvexHull;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.operation.buffer.BufferOp;
import org.apache.commons.math3.util.Pair;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author 290512
 * @create 2022-06-05 16:15
 */
@Service
public class BpServiceImpl implements BpService
{
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private IPresetsPositionService iPresetsPositionService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private RemoteNoticeInfoService remoteNoticeInfoService;

    @Autowired
    private PushService doPush;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private EmpLocationMapper empLocationMapper;

    @Autowired
    private WeatherDataMapper weatherDataMapper;

    @Autowired
    private BpGnssMapper bpGnssMapper;

    @Autowired
    private BpPositionDataMapper bpPositionDataMapper;
    @Autowired
    private BpAlarmMapper bpAlarmMapper;

    @Autowired
    private RemoteDictDataService remoteDictDataService;

    @Autowired
    private BpInfoAlarmMapper bpInfoAlarmMapper;


    //边坡告警时 摄像头对准附近点位
    // 查询边坡 告警数据 从告警信息中获取测点信息，然后从测点中拿到经纬度信息
    public String bpAlarmResetCamera(BpPositionData bpPositionData)
    {
        //将报警测点和各个经纬度进行对比，选取最近的经纬度对应的预置点
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        //发送信息
        setNotice(bpPositionData,geometryFactory);

        //获取摄像头的预置点的经纬度等信息
        PresetsPosition presetsPosition = new PresetsPosition();
        presetsPosition.setParentId(HkConstant.HK_CAMERAINDEXCODE);
        List<PresetsPosition> presetList = iPresetsPositionService.selectPresetsPositionList(presetsPosition);
        List<PresetsPosition> presets = presetList.stream().filter(item -> !"0".equals(item.getParentId())).collect(Collectors.toList());

        Point toSeePoint =geometryFactory.createPoint(new Coordinate(bpPositionData.getLng(),bpPositionData.getLat())); //告警测点经纬度
        List<Pair<String, Double>> distList = new ArrayList<>();
        for (int i = 0; i < presets.size(); i++)
        {
            PresetsPosition postion = presets.get(i);
            Point point =geometryFactory.createPoint(new Coordinate(Double.valueOf(postion.getLongitude()),Double.valueOf(postion.getLatitude())));
            Pair<String, Double> pair = new Pair(postion.getPresetPointindex(),toSeePoint.distance(point));
            distList.add(pair);
        }
        Optional<Pair<String, Double>> minDist = distList.stream().sorted(Comparator.comparing(t ->t.getValue())).findFirst();
        String preNum = "";
        if (minDist.isPresent())
        {
            preNum =  minDist.get().getKey();
        }
        JSONObject json = new JSONObject();
        json.put("command", "GOTO_PRESET");
        json.put("presetIndex",preNum);
        json.put("action",0);
        json.put("cameraIndexCode",HkConstant.HK_CAMERAINDEXCODE);
        //设置预置位
        HkResultUtlis.doPostStringArtemis(HkConstant.HK_CONTROLLING,json.toString());
        return preNum;
    }

    // 查询边坡 告警数据 从告警信息中获取测点信息，然后从测点中拿到经纬度信息

    public void setNotice(BpPositionData bpPositionData, GeometryFactory geometryFactory)
    {
        //TODO 人员定位数据获取失败
        //获取车辆定位信息
        List<DeviceInfoVo> allDeviceInfoVo = deviceService.getAllDeviceInfoVo();

        //获取人员位置信息，人员定位系统
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "%";
            List<EmpLocation> locationInfo = empLocationMapper.getLocationInfo(today);

            String remark="";
            String bpAreaType = bpPositionData.getBpAreaType();
            R<List<SysDictData>> monitoring_location_type = remoteDictDataService.getDictDataByDictType("bp_area_type");
            List<SysDictData> dictList = monitoring_location_type.getData();
            for (SysDictData dict : dictList)
            {
                //分类列表数据
                if (dict.getDictValue().equals(bpAreaType))
                {
                    remark = dict.getRemark();
                    break;
                }
            }
            List<BpAlarmVo> polygons = getPolygons(remark);
            for (int i = 0; i < polygons.size(); i++)
            {
                BpAlarmVo entry = polygons.get(i);
                List<BpPoint> points = entry.getArea();
                List<Coordinate> collect = points.stream().map(item ->
                        new Coordinate(item.getLon(), item.getLat())
                ).collect(Collectors.toList());
                Coordinate[] coordinates = new Coordinate[collect.size()];
                Polygon polygon = geometryFactory.createPolygon(collect.toArray(coordinates));
                List<BpInfoAlarm>  bpInfoAlarmList = new ArrayList<>();
                for (int j = 0; j < allDeviceInfoVo.size(); j++)
                {
                    BpInfoAlarm bpInfoAlarm = new BpInfoAlarm();
                    DeviceInfoVo item = allDeviceInfoVo.get(j);
                    if(item.getLongitude()!=null && item.getLatitude()!=null)
                    {
                        Point p = geometryFactory.createPoint(new Coordinate(item.getLongitude(), item.getLatitude()));
                        //判断车是否在所在区域内
                        if (polygon.contains(p))
                        {
                            Date date = new Date();
                            //通过deviceName获取userId
                            //TODO 人员定位数据获取失败  后期库通后 解开下方注释即可
//                            String userId = this.getUserIdByDeviceName(item.getDeviceName());
                            String userId = "1";
                            //插入告警信息 个推和 pc消息
                            executor.execute(() -> {
                                if(StringUtils.isNotEmpty(userId+""))
                                {
                                    //推送消息提醒
                                    this.noticeInfo( userId+"", date, bpPositionData.getAlertcontent());
                                }

                            });

                            bpInfoAlarm.setAlarmTime(date);
                            bpInfoAlarm.setContent(bpPositionData.getAlertcontent());
                            bpInfoAlarm.setUserId(Long.valueOf(userId));
                            bpInfoAlarmList.add(bpInfoAlarm);
                        }
                    }
                    if(bpInfoAlarmList!=null && bpInfoAlarmList.size()>0){
                        bpInfoAlarmMapper.insertBpInfoAlarmBatch(bpInfoAlarmList);
                    }

                }
            }

    }
    @Override
    public void  bpTest(){
        BpPositionData bpPositionData= new BpPositionData();
        bpPositionData.setBpAreaType("4");
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        String remark="";
        String bpAreaType = bpPositionData.getBpAreaType();
        R<List<SysDictData>> monitoring_location_type = remoteDictDataService.getDictDataByDictType("bp_area_type");
        List<SysDictData> dictList = monitoring_location_type.getData();
        for (SysDictData dict : dictList)
        {
            //分类列表数据
            if (dict.getDictValue().equals(bpAreaType))
            {
                remark = dict.getRemark();
                break;
            }
        }
        List<BpAlarmVo> polygons = getPolygons(remark);
        for (int i = 0; i < polygons.size(); i++)
        {
            BpAlarmVo entry = polygons.get(i);
            List<BpPoint> points = entry.getArea();
            List<Coordinate> collect = points.stream().map(item ->
                    new Coordinate(item.getLon(), item.getLat())
            ).collect(Collectors.toList());
            Coordinate[] coordinates = new Coordinate[collect.size()];
            Polygon polygon = geometryFactory.createPolygon(collect.toArray(coordinates));
            Point p = geometryFactory.createPoint(new Coordinate(119.587178, 45.497205d));
            //判断车是否在所在区域内
            if (polygon.contains(p))
            {
                System.out.println(polygon.contains(p));
            }
        }
    }
    public List<BpAlarmVo> getPolygons(String holeStr)
    {
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
            List<BpAlarmVo> list = new ArrayList<>();
            try
            {
                BpAlarmVo polygonMap = new BpAlarmVo();
                List<BpPoint> points = new ArrayList<>();
                JSONArray array = JSONArray.parseArray(holeStr);
                Coordinate[] c = new Coordinate[array.size()];
                for (int j = 0; j < array.size(); j++)
                {
                    JSONObject point = array.getJSONObject(j);
                    double lon = point.getDouble("longitude");
                    double lat = point.getDouble("latitude");
                    c[j] = new Coordinate(lon, lat);

                }
                //求解凸包
                ConvexHull ch = new ConvexHull(c, geometryFactory);
                Coordinate[] hull  = ch.getConvexHull().getCoordinates();
                Polygon plygon = geometryFactory.createPolygon(hull);
                Point center = plygon.getInteriorPoint();
                //外扩多少米
                Geometry waikuo =  getBufferedZipPolygon(plygon,0d);

                //获得外扩的米数
                Coordinate[] waiPolygon = waikuo.getCoordinates();
                points = Arrays.stream(waiPolygon).map(item -> new BpPoint(item.x, item.y)).collect(Collectors.toList());
                polygonMap.setArea(points);
                polygonMap.setLon(center.getX());
                polygonMap.setLat(center.getY());
                list.add(polygonMap);

            } catch (FactoryException e)
            {
                e.printStackTrace();
            } catch (TransformException e)
            {
                e.printStackTrace();
            }

        return list;
    }
    /**
     * @param zipPolygon  原始多边形
     * @param distanceMeters  外扩距离 米
     * @return 外扩后的多边行
     * @throws FactoryException
     * @throws TransformException
     */
    private Geometry getBufferedZipPolygon(Polygon zipPolygon, double distanceMeters) throws FactoryException, TransformException
    {
        CoordinateReferenceSystem wgsCRS;
        CoordinateReferenceSystem distanceMetersCRS;
        try {
            wgsCRS = CRS.decode("EPSG:4326", true);
            distanceMetersCRS = CRS.decode("EPSG:3857", true);

            MathTransform transform = CRS.findMathTransform(wgsCRS, distanceMetersCRS);
            MathTransform reverseTransform = CRS.findMathTransform(distanceMetersCRS, wgsCRS);

            Geometry transformedZipPolygon = JTS.transform(zipPolygon, transform);
            //不可设置样式的
//            Geometry bufferZipPolygon = transformedZipPolygon.buffer(distanceMeters);

            //可设置样式的
            BufferOp bufOp = new BufferOp(transformedZipPolygon);
            bufOp.setEndCapStyle(BufferOp.CAP_SQUARE);
            Geometry bufferZipPolygon = bufOp.getResultGeometry(distanceMeters);

            return JTS.transform(bufferZipPolygon, reverseTransform);
        } catch (FactoryException e) {
            e.printStackTrace();
        } catch (TransformException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过设备名称获取userId
     * @param deviceName 设备名称
     * @return userId
     */
    public String getUserIdByDeviceName(String deviceName){
        int account = weatherDataMapper.selectAccountByDeviceName(deviceName);
        if(account!=0){
            String userName =  String.format("%08d", account);
            R<LoginUser> loginUser = remoteUserService.getUserInfo(userName, SecurityConstants.INNER);
            if(loginUser!=null && loginUser.getData().getSysUser().getUserId()!=null){
                return loginUser.getData().getSysUser().getUserId().toString();
            }
        }
        return "";
    }
    /**
     * 推送消息
     */
    public void noticeInfo(String userId, Date date, String alarm){
        SysNoticeInfo info = new SysNoticeInfo();
        info.setContent(alarm);
        info.setTitle("边坡位移告警");
        info.setUserId(userId);
        info.setCreateBy(userId);
        info.setCreateTime(date);
        remoteNoticeInfoService.add(info);
        String cid = remoteUserService.getUserById(new Long(userId)).getData().getCid();
        Integer clientType = remoteUserService.getUserById(new Long(userId)).getData().getClientType();
        if(StringUtils.isNotEmpty(cid)){
            doPush.push(
                    cid,
                    "边坡位移告警",
                    alarm
                    ,clientType
            );
        }
    }
    //接口返回Gnss实时边坡数据
    public List<BpGnss> getBpGnssList()
    {

        List<BpGnss> list = new ArrayList<>();
        String reporttime = bpGnssMapper.selectNowReporttime();
        if(StringUtils.isEmpty(reporttime)){
            reporttime = "2022-06-01 00:00:00";
        }
        Date format = DateUtil.format(reporttime, "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtil.format(format, "yyyy-MM-dd HH:mm:ss");
        String endTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

        List<BpPositionData> bpPositionData = bpPositionDataMapper.selectBpPositionDataList();
        String names = bpPositionData.stream().map(BpPositionData::getName).collect(Collectors.joining(","));
        // 入参实体
        // 实体转JSON字符串
        String jsonString = "{\"appcode\":\""+BpConstant.APP_CODE+"\"}";
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("mode","get_gnss");
        contentMap.put("pointname",names);

        contentMap.put("starttime",startTime);
        contentMap.put("endtime",endTime);
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appcode",jsonString);
        String result = HttpClientUtil.postMap(BpConstant.BP_URL, headerMap, contentMap);
        JSONObject resultJson = JSONObject.parseObject(result);
        if("200".equals(resultJson.getString("code"))){
            JSONArray jsonArray = resultJson.getJSONArray("result");
            if(jsonArray!=null && jsonArray.size()>0){
                for(int i=0;i<jsonArray.size();i++){
                    JSONArray  jsonArray2 = jsonArray.getJSONArray(i);
                    if(jsonArray2!=null && jsonArray2.size()>0){
                        List<BpGnss>  spicList= jsonArray2.toJavaList(BpGnss.class);
                        list.addAll(spicList);
                    }
                }
            }
        }
        return list;
    }

    //接口返回边坡测点数据
    public List<BpPositionData> getBpPositionDataList()
    {
        List<BpPositionData> list = new ArrayList<>();
        // 入参实体
        // 实体转JSON字符串
        String jsonString = "{\"appcode\":\""+BpConstant.APP_CODE+"\"}";
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("mode","get_position_data");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appcode",jsonString);
        String result = HttpClientUtil.postMap(BpConstant.BP_URL, headerMap, contentMap);
        JSONObject resultJson = JSONObject.parseObject(result);
        if("200".equals(resultJson.getString("code"))){
            JSONArray jsonArray = resultJson.getJSONArray("result");
            List<BpPositionData>  spicList= jsonArray.toJavaList(BpPositionData.class);
            return spicList;
        }
        return list;
    }
    @Override
    public void saveBpGnss()
    {
        String reporttime = bpGnssMapper.selectNowReporttime();
        if(StringUtils.isEmpty(reporttime)){
            reporttime = "2022-06-01 00:00:00";
        }
        bpGnssMapper.deleteBpGnssBatchByTime(reporttime);
        List<BpGnss> bpGnssList = getBpGnssList();
        if(bpGnssList!=null && bpGnssList.size()>0){

            bpGnssMapper.insertBpGnssBatch(bpGnssList);
        }
    }

    @Override
    public void saveBpPositionData()
    {
        List<BpPositionData> bpPositionDataList = getBpPositionDataList();
        for (BpPositionData bpPositionData : bpPositionDataList)
        {
            String name = bpPositionData.getName();
            String lat = bpPositionData.getLat()+"";
            String lng = bpPositionData.getLng()+"";
            BpPositionData bp = bpPositionDataMapper.selectBpPositionDataById(name);
            if(bp!=null){
               String bLat =  bp.getLat()+"";
               String bLng = bp.getLng()+"";
               if(!bLat.equals(lat) || !bLng.equals(lng)){
                   bpPositionDataMapper.updateBpPositionDataByName(bpPositionData);
               }
            }
        }
    }

    @Override
    public void saveBpAlarm()
    {
        //接口获取报警
        List<BpAlarm> bpAlarm = getBpAlarm();
        if(bpAlarm!=null && bpAlarm.size()>0){
            bpAlarmMapper.insertBpAlarmBatch(bpAlarm);
        }
    }

    @Override
    public List<BpGnss> queryDataList(String gpsType, String dayType)
    {
        LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
        String startTime ="";
        String endTime2 ="";
        Date endTime = new Date();

        if("today".equals(dayType)){
            startTime =today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            // 实际数据
            endTime2 = DateUtil.format(endTime,"yyyy-MM-dd HH:mm:ss");

        }else if("yesterday".equals(dayType)){
            LocalDateTime yest_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).minusDays(1);
            String yestStartTime =yest_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            LocalDateTime yest_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).minusDays(1);//当天零点
            String yestEndTime =yest_end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            // 实际数据
            startTime = yestStartTime; //昨天的零点
            endTime2 = yestEndTime; //昨天的最大点
        }

        List<BpGnss>  list = bpGnssMapper.selectpGnssByTime(gpsType,startTime,endTime2);
        return list;
    }
    @Override
    public Map<String, String> bpAlarmRemark(String dayType)
    {
        return null;
    }

    @Override
    public List<Map<String,Object>> bpCdData()
    {
        List<Map<String,Object>> listMap = new ArrayList<>();
        List<BpPositionData> bpPositionData = bpPositionDataMapper.selectBpPositionDataList();
        for (BpPositionData bpPositionDatum : bpPositionData)
        {
            Map<String,Object> map = new HashMap<>();
            map.put("name",bpPositionDatum.getName());
            map.put("lat",bpPositionDatum.getLat());
            map.put("lng",bpPositionDatum.getLng());
            map.put("type","1");
            listMap.add(map);
        }
        return listMap;
    }

    @Override
    public List<BpAlarm> selectBpAlarmList(BpAlarm bpAlarm)
    {
        List<BpAlarm> bpAlarmsList = bpAlarmMapper.selectBpAlarmList(bpAlarm);
        return bpAlarmsList;
    }

    @Override
    public BpAlarm selectBpAlarmById(String id)
    {
        return bpAlarmMapper.selectBpAlarmById(id);
    }


    public List<BpAlarm> getBpAlarm(){
        List<BpAlarm> list = new ArrayList<>();
        // 入参实体
        // 实体转JSON字符串
        String jsonString = "{\"appcode\":\""+BpConstant.APP_CODE+"\"}";
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("mode","get_alarm");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appcode",jsonString);
        String result = HttpClientUtil.postMap(BpConstant.BP_URL, headerMap, contentMap);
        JSONObject resultJson = JSONObject.parseObject(result);
        if("200".equals(resultJson.getString("code"))){
            if(StringUtils.isEmpty(resultJson.getString("result"))){
                return list;
            }
            JSONArray jsonArray = resultJson.getJSONArray("result");
            if(jsonArray!=null && jsonArray.size()>0){
                List<BpAlarm>  spicList= jsonArray.toJavaList(BpAlarm.class);
                List<BpAlarm> bpAlarmsList = coverBpAlarmList(spicList);
                return bpAlarmsList;
            }
        }
        return list;
    }

    public List<BpAlarm> coverBpAlarmList(List<BpAlarm> bpAlarmsList){
        List<BpAlarm> list = new ArrayList<>();
        for (BpAlarm bpAlarm : bpAlarmsList)
        {
            BpAlarm bpAlarmBean = bpAlarmMapper.selectBpAlarmById(bpAlarm.getId());
            if(bpAlarmBean==null){
                BpAlarm bp = new BpAlarm();
                BpPositionData bpPositionData = bpPositionDataMapper.selectBpPositionDataById(bpAlarm.getName());
                BeanUtils.copyProperties(bpAlarm,bp);
                bp.setLat(bpPositionData.getLat());
                bp.setLon(bpPositionData.getLng());
                //报警内容
                bpPositionData.setAlertcontent(bpAlarm.getAlertcontent());
                //有报警 发送推送消息及摄像头对准测点
                bpAlarmResetCamera(bpPositionData);
                list.add(bp);
            }
        }
        return list;
    }

    @Override
    public BpPositionData selectBpPositionDataByName(String name)
    {
        return bpPositionDataMapper.selectBpPositionDataByName(name);
    }

    @Override
    public List<BpPositionData> selectBpPositionDataList(BpPositionData bpPositionData)
    {
        return bpPositionDataMapper.selectBpPositionDataListConfig(bpPositionData);
    }

    @Override
    public int updateBpPositionData(BpPositionData bpPositionData)
    {
        return bpPositionDataMapper.updateBpPositionData(bpPositionData);
    }

    @Override
    public JSONObject getBpAlarms()
    {
        // 实体转JSON字符串
        String jsonString = "{\"appcode\":\""+BpConstant.APP_CODE+"\"}";
        Map<String, String> contentMap = new HashMap<>();
        contentMap.put("mode","get_alarm");
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appcode",jsonString);
        String result = HttpClientUtil.postMap(BpConstant.BP_URL, headerMap, contentMap);
        JSONObject resultJson = JSONObject.parseObject(result);
        if("200".equals(resultJson.getString("code"))){
//            if(StringUtils.isEmpty(resultJson.getString("result"))){
//                resultJson.put("total",0);
//                List<BpAlarmTest> bpAlarmTestList = new ArrayList<>();
//                BpAlarmTest b = new BpAlarmTest();
//                b.setId("57790");
//                b.setName("NK-01");
//                b.setAlerttime(DateUtil.format("2022-06-08 09:00:00"));
//                b.setAlertlevel("1");
//                b.setAlertcontent("霍林郭勒南露天煤矿项目，NK-01监测点产生1级报警，报警内容X位移值大于1mm，当前为1.6mm，报警时间2022-06-08，请火速处理！！！");
//                bpAlarmTestList.add(b);
//                resultJson.put("result",bpAlarmTestList);
//            }
            JSONArray jsonArray = resultJson.getJSONArray("result");
            if(jsonArray!=null && jsonArray.size()>0){
                resultJson.put("total",jsonArray.size());
            }
        }
        return resultJson;
    }
}
